//
//  SPPartnerViewController.m
//  SuperPatrner
//
//  Created by EvanZ on 15/3/15.
//  Copyright (c) 2015年 EvanZ. All rights reserved.
//

#import "SPPartnerViewController.h"
#import "SPPartnerInfoViewController.h"
#import "SPHTTPRequestOperationManager.h"

@interface SPPartnerViewController (){
    UITableView * m_tableView;
    NSMutableArray * m_itemMutableArray;
}

@end

@implementation SPPartnerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loginSuccess) name:NOTI_LOGIN_SUCC object:nil];


    NSDictionary * dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"Partner",@"account_name", nil];
    m_itemMutableArray = [[NSMutableArray alloc]initWithObjects:dic,dic,dic, nil];
    
    m_tableView = [[UITableView alloc]initWithFrame:self.view.frame];
    m_tableView.delegate = self;
    m_tableView.dataSource = self;
    [self.view addSubview:m_tableView];
    m_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self getPartner];
}

- (void)loginSuccess{
}

- (void)getPartner{
    SPHTTPRequestOperationManager *manager = [SPHTTPRequestOperationManager manager];
    [manager getPartnersInfoWithID:USER_ID Success:^(AFHTTPRequestOperation *operation, id responseObject) {
        m_itemMutableArray = [[NSMutableArray alloc]initWithArray:[responseObject objectForKey:@"partners"]];
        [m_tableView reloadData];
        NSLog(@"get_partner_info>>>>>>>>SUCCESS: %@",responseObject);
        
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"get_partner_info>>>>>>>>Error: %@", error);
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [m_itemMutableArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 180.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dataDic = [m_itemMutableArray objectAtIndex:indexPath.row];
    
    MyPartnerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[MyPartnerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    [cell setCellInfo:dataDic];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [m_itemMutableArray removeObjectAtIndex:indexPath.row];
        // Delete the row from the data source.
        [m_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        //[[EZDatabase shareDB]deleteFromTable:@"image" WithID:[[itemMutableArray objectAtIndex:indexPath.row] valueForKey:@"ID"]];
        
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"Delete";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * dataDic = [m_itemMutableArray objectAtIndex:indexPath.row];

    SPPartnerInfoViewController * vc = [[SPPartnerInfoViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.m_id = [dataDic valueForKey:@"id"];
    [self.navigationController pushViewController:vc animated:YES];
}

@end


@implementation MyPartnerCell
@synthesize m_titleLabel;
@synthesize m_avatarImageView;
@synthesize m_userInfoTextLabel;
@synthesize m_signinInfoTextLabel;
@synthesize m_signinTextLabel;
@synthesize m_detailInfoTextLabel;
@synthesize m_detailTextLabel;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        m_titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 300, 20)];
        [m_titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
        [m_titleLabel setBackgroundColor:[UIColor clearColor]];
        [self addSubview:m_titleLabel];
        
        m_avatarImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 10, 40, 40)];
        [m_avatarImageView setImage:[UIImage imageNamed:@"avatar"]];
        [self addSubview:m_avatarImageView];
        
        int xOffset = m_avatarImageView.frame.origin.x + m_avatarImageView.frame.size.width + 10;
        m_userInfoTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(xOffset, 10, self.frame.size.width - xOffset - 20 , 45)];
        [m_userInfoTextLabel setFont:[UIFont systemFontOfSize:15]];
        [m_userInfoTextLabel setBackgroundColor:[UIColor clearColor]];
        [m_userInfoTextLabel setText:@"会员ID：      昵称："];
        [self addSubview:m_userInfoTextLabel];
        
        xOffset = 30;
        m_signinTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(xOffset, m_userInfoTextLabel.frame.origin.y + m_userInfoTextLabel.frame.size.height , self.frame.size.width - xOffset - 20 , 30)];
        [m_signinTextLabel setFont:[UIFont systemFontOfSize:15]];
        [m_signinTextLabel setBackgroundColor:[UIColor clearColor]];
        [m_signinTextLabel setText:@"注册                     最后登录"];
        [self addSubview:m_signinTextLabel];
        
        m_signinInfoTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(xOffset, m_signinTextLabel.frame.origin.y + m_signinTextLabel.frame.size.height -5 , self.frame.size.width - xOffset - 20 , 30)];
        [m_signinInfoTextLabel setFont:[UIFont systemFontOfSize:12]];
        [m_signinInfoTextLabel setBackgroundColor:[UIColor clearColor]];
        [m_signinInfoTextLabel setText:@""];
        [self addSubview:m_signinInfoTextLabel];
        
        xOffset = 25;
        m_detailTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(xOffset, m_signinInfoTextLabel.frame.origin.y + m_signinInfoTextLabel.frame.size.height -5, self.frame.size.width - xOffset - 20 , 30)];
        [m_detailTextLabel setFont:[UIFont systemFontOfSize:15]];
        [m_detailTextLabel setBackgroundColor:[UIColor clearColor]];
        [m_detailTextLabel setText:@"合伙人          本月收益          距离升级"];
        [self addSubview:m_detailTextLabel];
        
        m_detailInfoTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(xOffset, m_detailTextLabel.frame.origin.y + m_detailTextLabel.frame.size.height -5, self.frame.size.width - xOffset - 20 , 30)];
        [m_detailInfoTextLabel setFont:[UIFont systemFontOfSize:15]];
        [m_detailInfoTextLabel setBackgroundColor:[UIColor clearColor]];
        [m_detailInfoTextLabel setText:@"111w"];
        [self addSubview:m_detailInfoTextLabel];
        
        UIView * backColorView = [[UIView alloc]initWithFrame:CGRectMake(0, m_detailInfoTextLabel.frame.origin.y + m_detailInfoTextLabel.frame.size.height+5, self.frame.size.width, 15)];
        [backColorView setBackgroundColor:[UIColor colorWithRed:239.0/255.0 green:239.0/255.0 blue:244.0/255.0 alpha:1.0]];
        [self addSubview:backColorView];
        
        UIView * backLineView = [[UIView alloc]initWithFrame:CGRectMake(0, m_detailInfoTextLabel.frame.origin.y + m_detailInfoTextLabel.frame.size.height+5, self.frame.size.width, 1)];
        [backLineView setBackgroundColor:[UIColor colorWithRed:229.0/255.0 green:229.0/255.0 blue:234.0/255.0 alpha:1.0]];
        [self addSubview:backLineView];
        
        self.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    return self;
}

- (void)setImageView:(UIImage*)image
{
    m_avatarImageView.image = image;
    [m_avatarImageView setFrame:CGRectMake(20, 10, 40 , 40)];
}

- (void)setImageUrl:(NSString *)urlStr{
    //NSURL * url = [NSURL URLWithString:[[BASED_URL stringByAppendingString:@"/"] stringByAppendingString:urlStr]];
    //[m_imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"food_loading"]];
}

- (void)setCellInfo:(NSDictionary *)info{
    [m_userInfoTextLabel setText:[NSString stringWithFormat:@"会员ID：%@      昵称：%@",
                                 [info valueForKey:@"id"],
                                 [info valueForKey:@"account_name"]]];
    [m_signinInfoTextLabel setText:[NSString stringWithFormat:@"%@",[info valueForKey:@"register_time"]]];

}

@end


